package org.example.experiment;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
 * 反射示例
 */
public class ReflectExperiment {
    public static void main(String[] args) {
        invokeSayHello("org.example.experiment.ReflectExperiment$ExampleInside");
        invokeSayHello("org.example.experiment.ExampleOutside");
    }

    public static void invokeSayHello(String path) {
        try {
            Class<?> clazz = Class.forName(path);
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            Object instance = constructor.newInstance();
            Method method = instance.getClass().getMethod("sayHello", String.class);
            method.invoke(instance, "2024");
        } catch (Exception e) {
            System.out.println(e);
        }
    }

    static class ExampleInside {
        public ExampleInside(){};
        public void sayHello(String msg) {
            System.out.println("ExampleInside say hello " + msg);
        }
    }
}

class ExampleOutside {
    public ExampleOutside(){};
    public void sayHello(String msg) {
        System.out.println("ExampleOuter say hello " + msg);
    }
}
